//
//  UIButton+Extension.m
//  
//
//  Created by 王林峰 on 16/4/11.
//
//

#import "UIButton+Extension.h"
#import "UIImage+Extension.h"
@implementation UIButton (Extension)
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case MKButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case MKButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}
/**
 *  UIButton文字图片垂直显示
 *
 *  @param image    图片
 *  @param title    文字
 *  @param target   self
 *  @param selector 点击时间
 *
 *  @return 返回button
 */
+ (UIButton*)createButtonWithImage:(NSString *)image Title:(NSString *)title Target:(id)target Selector:(SEL)selector{
    UIButton * button = [UIButton new];
    //    [button setFrame:frame];
    UIImage *Image = [[UIImage imageWithName:image] scaleImageWithSize:CGSizeMake(35, 35)];
    [button setImage:Image forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:nil] forState:UIControlStateHighlighted];
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:nil forState:UIControlStateHighlighted];
        button.titleLabel.font =[UIFont systemFontOfSize:14 ];
        
    }
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
    //设置内容垂直或水平显示位置
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 18, 20, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(40,-35, 0, 0);
    
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}
+ (UIButton*)createCenterButtonWithFrame: (CGRect) frame image:(NSString *)image Title:(NSString *)title Target:(id)target Selector:(SEL)selector  size:(CGSize)size font:(CGFloat)font {
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:nil forState:UIControlStateHighlighted];
    }
    if (font>0) {
        button.titleLabel.font =[UIFont systemFontOfSize:font];
    }
    if (size.width>0) {
         [button setImage:[[UIImage imageNamed:image] scaleImageWithSize:size] forState:UIControlStateNormal];
    } else {
         [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
    //设置内容垂直或水平显示位置
    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
    
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}
////九宫格中心按钮
//+ (UIButton*) createCenterButtonWithFrame: (CGRect) frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector {
//    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//    [button setFrame:frame];
//    
//    if (title) {
//        [button setTitle:title forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        [button setTitleColor:nil forState:UIControlStateHighlighted];
//        button.titleLabel.font =[UIFont systemFontOfSize:10];
//        
//    }
//    UIImage *image = [kLoadImagePlacehold circleImageWithBorderWidth:2 borderColor:[UIColor lightGrayColor]];
//    [button setImage:image forState:UIControlStateNormal];
//
//    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
//    //设置内容垂直或水平显示位置
//    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:4];
//    
//    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
//    
//    return button;
//}
+ (UIButton*) createButtonWithFrame: (CGRect) frame Image:(NSString *)image Title:(NSString *)title Target:(id)target Selector:(SEL)selector{
    UIButton * button = [UIButton new];
    [button setFrame:frame];
    UIImage *Image = [[UIImage imageWithName:image] scaleImageWithSize:CGSizeMake(40, 40)];
    [button setImage:Image forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:nil] forState:UIControlStateHighlighted];
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:nil forState:UIControlStateHighlighted];
        button.titleLabel.font =[UIFont systemFontOfSize:13];
        
    }
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
    //设置内容垂直或水平显示位置
    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:4];
    
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    if (selector&&target) {
        [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    }
    
    return button;
    
}

/**
 *  图片按钮
 *
 *  @param frame        尺寸
 *  @param target       self
 *  @param selector     点击事件
 *  @param image        前景颜色
 *  @param imagePressed 高亮颜色
 *
 *  @return 返回button
 */
+ (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/**
 *  文字按钮
 *
 *  @param frame    尺寸
 *  @param title    标题
 *  @param target   self
 *  @param selector 点击事件
 *
 *  @return 返回button
 */
+ (UIButton *) createButtonWithFrame:(CGRect)frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    return button;
}
+ (UIButton *)createNormalButtonWithFrame:(CGRect)frame Image:(NSString *)image Title:(NSString *)title font:(CGFloat)font imageSize:(CGSize)imageSize {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    if (image) {
        UIImage *temp = [UIImage imageNamed:image];
        if (imageSize.width>0) {
            temp = [temp scaleImageWithSize:imageSize];
        }
        [button setImage:temp forState:UIControlStateNormal];
    }
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (font>0) {
        button.titleLabel.font = [UIFont systemFontOfSize:font];
    }
    return button;
}

+ (UIButton *) createButtonWithTitle:(NSString *)title Image:(NSString *)image Target:(id)target Selector:(SEL)selector{
    UIButton * button = [UIButton new];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0,16, 0, 0)];
    [button setImage:[UIImage imageWithName:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:nil] forState:UIControlStateHighlighted];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton *)createSimpleButtonWithFrame:(CGRect)frame Title:(NSString *)title Image:(NSString *)image Target:(id)target Selector:(SEL)selector{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (image) {
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
//    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
//    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    return button;
}


//设置UIButton长用的属性用于简化代码
+(UIButton *)setButtonWithFrame:(CGRect)frame Title:(NSString *)title Image:(NSString *)image Font:(CGFloat)font  Color:(UIColor *)color Target:(id)target Selector:(SEL)selector
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    if (title == nil) {
        
    }
    else {
       [button setTitle:title forState:UIControlStateNormal];
    }
    [button setTitleColor:color  forState:UIControlStateNormal];
    //如果等于nil 不用设置图片
    if (image) {
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    [button.titleLabel setFont:[UIFont systemFontOfSize:font]];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return  button;
}


@end
